import {Component, EventEmitter, Input, Output} from '@angular/core';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ConfigProvider} from "../../providers/config/config";
import {UtilsProvider} from "../../providers/utils/utils";

@Component({
    selector: 'comment',
    templateUrl: 'comment.html'
})
export class CommentComponent {
    @Input() showType:boolean = true;
    @Output() private outer=new EventEmitter();
    commentData = {
        show: false,
        amoy_id: 0,
        reply_uid: 0,
        token: '',
        reply_id: 0,
        index: 0
    };
    formData = {
        type: 1,
        content: ''
    };
    holderText = '说说你的看法';
    commentResult:boolean = false;
    constructor(
        public httpService: HttpServicesProvider,
        public config: ConfigProvider,
        public util: UtilsProvider
    ) {

    }

    /**
     * 点击评论显示评论框
     * @param amoy_id 回复信息的ID
     * @param reply_user 回复对象
     * @param index 索引
     * */
    showComment(amoy_id, reply_user={user_id:0,nick_name:'',reply_id: 0}, index=0){
        if(reply_user['user_id'] == this.config.getUserId()){
            this.util.presentAlert('您不能给自己回复');
            return false;
        }
        this.holderText = reply_user['nick_name'] == '' ? '说说你的看法' : '回复' + reply_user['nick_name'] + '：';
        this.commentData.show = true;
        this.commentData.reply_id = reply_user['reply_id'];
        this.commentData.reply_uid = reply_user['user_id'];
        this.commentData.amoy_id = amoy_id;
        this.commentData.token = this.config.getToken();
        this.commentData.index = index;
    }

    /**
     * 关闭评论框
     * */
    close(){
        this.commentData.show = false;
        this.formData = {
            type: 1,
            content: ''
        }; 
    }

    /**
     * 发布评论
     * */
    send(){
        let apiUrl = "/comment/add_comment_handle";
        this.httpService.requestPostData(apiUrl,{
            amoy_id: this.commentData.amoy_id,
            token: this.commentData.token,
            p_id: this.commentData.reply_uid,
            reply_id: this.commentData.reply_id,
            content: this.formData.content,
            type: this.formData.type
        },(response)=>{
            if(response.error == 200){
                this.commentResult = true;
                this.outer.emit(this.commentData);
            }
            this.close();
        })
    }


}
